package com.nhjf.service.impl.teachers;

import com.nhjf.command.coursesSeries.CoursesSeriesCommand;
import com.nhjf.dao.coursePicture.CoursePictureDao;
import com.nhjf.dao.coursesSeries.CoursesSeriesDao;
import com.nhjf.dao.teachers.CoursesDao;
import com.nhjf.dao.teachers.TeachersDao;
import com.nhjf.model.coursePicture.CoursePicture;
import com.nhjf.model.coursesSeries.CoursesSeries;
import com.nhjf.model.teachers.Courses;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.coursesSeries.CoursesSeriesQo;
import com.nhjf.pojo.qo.teachers.CoursesQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.impl.BaseServiceImpl;
import com.nhjf.service.teachers.CoursesService;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service("coursesService")
public class CoursesServiceImpl extends BaseServiceImpl<Courses,CoursesQo,CoursesDao> implements CoursesService {
	@Autowired
	private CoursesDao CoursesDao;

	@Autowired
	private CoursesSeriesDao seriesDao;
	@Autowired
	private TeachersDao teacherDao;
	@Autowired
	private CoursePictureDao coursePictureDao;

	@Override
	protected CoursesDao getDao() {
		return CoursesDao;
	}

	//课程查询、修改、添加
	@Override
	public ServiceResultDto findadd(CoursesQo courses) {

		ServiceResultDto resultDto = new ServiceResultDto();
		Courses Addtocourses=new Courses();
		if(courses==null){
			resultDto.setSuccess(false);
			resultDto.setResultMessage("非法操作");
			return resultDto;
		}

		//课程标题
		if(courses.getTotalTitle()!=null){
			Addtocourses.setCoursetitle(courses.getTotalTitle());
		}else {
			resultDto.setResultMessage("课程标题为空");
			resultDto.setSuccess(false);
			return resultDto;
		}

		//        //二级课程标题
		//        if(courses.getCoursetitleTwo()!=null){
		//            Addtocourses.setCoursetitleTwo(courses.getCoursetitleTwo());
		//        }
		//封面
		//        if(courses.getImge()!=null){
		//            Addtocourses.setImge(courses.getImge());
		//        }else{
		//        	resultDto.setResultMessage("封面为空");
		//            resultDto.setSuccess(false);
		//            return resultDto;
		//        }
		//课程视频
		if(courses.getVideo()!=null){
			Addtocourses.setVideo(courses.getVideo());
		}else{
			resultDto.setResultMessage("视频为空");
			resultDto.setSuccess(false);
			return resultDto;
		}
		//        //是否付费
		if(courses.getIsPrice()==1||courses.getIsPrice()==2){
			Addtocourses.setIsPay(courses.getIsPrice());
		}

		//讲师
		//        if(courses.getTeacher()!=null){
		//            Addtocourses.setTeacher(courses.getTeacher());
		//        }
		//一级分类
		//        if(courses.getCourseTypeOne()!=null){
		//            Addtocourses.setCourseTypeOneId(courses.getCourseTypeOne());
		//        }
		//二级分类
		//        if(courses.getCourseTypeTwo()!=null){
		//            Addtocourses.setCourseTypeTwoId(courses.getCourseTypeTwo());
		//        }
		//课程共享
		//        if(courses.getCourseShare()==1||courses.getCourseShare()==2||courses.getCourseShare()==3){
		//            Addtocourses.setCourseShare(courses.getCourseShare());
		//        }
		//课程简介
		//        if(courses.getCourseintroduction()!=null){
		//            Addtocourses.setCourseintroduction(courses.getCourseintroduction());
		//        }else{
		//        	resultDto.setResultMessage("课程简介为空");
		//            resultDto.setSuccess(false);
		//            return resultDto;
		//        }
		if(StringUtils.isNotBlank(courses.getViewTime())) {
			Addtocourses.setViewTime(courses.getViewTime());
		}
		if(courses.getUploaderId()!=null) {
			TeachersQo teachersQo=new TeachersQo();

			teachersQo.setSeachOrganization(true);
			teachersQo.setSeachUser(true);
			teachersQo.setTeacherId(courses.getUploaderId());
			Teacher teacher=teacherDao.queryUnique(teachersQo);
			//        	teacher.setCoursesNum(teacher.getCoursesNum()+1);
			//        	Addtocourses.setUploader(teacher);
			//        	teacherDao.update(teacher);
			Addtocourses.setUploader(teacher);
		}
		if(courses.getIsReview()!=null) {
			Addtocourses.setIsReview(courses.getIsReview());
		}else{
			resultDto.setResultMessage("IsReview为空");
			resultDto.setSuccess(false);
			return resultDto;
		}
		if(courses.getSeriesId()!=null) {
			CoursesSeriesQo setid=new CoursesSeriesQo();
			setid.setId(courses.getSeriesId());
			Addtocourses.setId(courses.getSeriesId());
			CoursesSeries coursesSeries= seriesDao.queryUnique(setid);
			Addtocourses.setCoursesSeries(coursesSeries);
		}
		Addtocourses.setReleasetime(new Date());
		CoursesDao.save(Addtocourses);
		resultDto.setSuccess(true);
		return resultDto;
	}

	//修改
	@Override
	public ServiceResultDto findpmodify(CoursesQo courses) {
		ServiceResultDto resultDto = new ServiceResultDto();
		
		CoursesQo cou =new CoursesQo();
		cou.setSeachCoursesSeries(true);
		cou.setSeachUploader(true);
		cou.setCoursesId(courses.getCoursesId());
		Courses Addtocourses=CoursesDao.queryUnique(cou);
		CoursesSeriesQo coursesSeries=new CoursesSeriesQo();
		coursesSeries.setCoursesSeriesId(Addtocourses.getCoursesSeries().getId());
		CoursesSeries coursesSeries2= seriesDao.queryUnique(coursesSeries);
		if(courses==null){
			resultDto.setSuccess(false);
			resultDto.setResultMessage("非法操作");
			return resultDto;
		}
		//课程标题
		if(courses.getTotalTitle()!=null){
			coursesSeries2.setTotalTitle(courses.getTotalTitle());
			Addtocourses.setCoursetitle(courses.getTotalTitle());
		}else {
			resultDto.setResultMessage("课程标题为空");
			resultDto.setSuccess(false);
			return resultDto;
		}
		//二级课程标题
		//         if(courses.getCoursetitleTwo()!=null){
		//             Addtocourses.setCoursetitleTwo(courses.getCoursetitleTwo());
		//         }
		//封面
		//         if(courses.getImge()!=null){
		//             Addtocourses.setImge(courses.getImge());
		//         }else{
		//         	resultDto.setResultMessage("封面为空");
		//             resultDto.setSuccess(false);
		//             return resultDto;
		//         }
		if(StringUtils.isNotBlank(courses.getViewTime())) {
			Addtocourses.setViewTime(courses.getViewTime());
		}
		//课程视频
		if(courses.getVideo()!=null){
			Addtocourses.setVideo(courses.getVideo());
		}
		//         //是否付费
		//         if(courses.getIsPay()==1){
		//             Addtocourses.setIsPay(courses.getIsPay());
		//         }else if(courses.getIsPay()==2) {
		//        	 Addtocourses.setIsPay(courses.getIsPay());
		//        	 if(courses.getPay()==null) {
		//                 resultDto.setSuccess(false);
		//                 resultDto.setResultMessage("付费课程 必须添费用！");
		//                 return resultDto;
		//        	 }else {
		//        		 Addtocourses.setPay(courses.getPay());
		//        	 }
		//         }

		//讲师
		//         if(courses.getTeacher()!=null){
		//             Addtocourses.setTeacher(courses.getTeacher());
		//         }
		//一级分类
		//         if(courses.getCourseTypeOne()!=null){
		//             Addtocourses.setCourseTypeTwoId(courses.getCourseTypeOne());
		//         }
		//二级分类
		//         if(courses.getCourseTypeTwo()!=null){
		//             Addtocourses.setCourseTypeTwoId(courses.getCourseTypeTwo());
		//         }
		//课程共享
		//         if(courses.getCourseShare()==1||courses.getCourseShare()==2||courses.getCourseShare()==3){
		//             Addtocourses.setCourseShare(courses.getCourseShare());
		//         }
		//课程简介
		//         if(courses.getCourseintroduction()!=null){
		//             Addtocourses.setCourseintroduction(courses.getCourseintroduction());
		//         }else{
		//         	resultDto.setResultMessage("课程简介为空");
		//             resultDto.setSuccess(false);
		//             return resultDto;
		//         }
		if(courses.getSeriesId()!=null) {
			
			coursesSeries2.setIsSeriesLesson(courses.getSeriesId().intValue());
			
		}
		if(courses.getIsPrice()==1 || courses.getIsPrice()==2) {
			coursesSeries2.setIsReview(0);
			Addtocourses.setIsReview(0);
			Addtocourses.setIsPay(courses.getIsPrice());
			coursesSeries2.setIsPrice(courses.getIsPrice());
		}
		CoursesDao.update(Addtocourses);
		seriesDao.update(coursesSeries2);
		resultDto.setSuccess(true);
		return resultDto;
	}

	@Override
	public ServiceResultDto findaCommand(CoursesSeriesCommand command) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		CoursesSeries coursesSeries=new CoursesSeries();
		Courses courses=new Courses();
		if(command==null) {
			resultDto.setResultMessage("非法操作");
			resultDto.setSuccess(false);
			return resultDto;
		}
		if(command.getCourseSecondaryTitle()!=null) {
			coursesSeries.setCourseSecondaryTitle(command.getCourseSecondaryTitle());
		}
		if(command.getCourseShare()!=null) {
			coursesSeries.setCourseShare(command.getCourseShare());
		}
		if(command.getCourseTypeOne()!=null) {
			coursesSeries.setCourseTypeOne(command.getCourseTypeOne());
		}
		if(command.getCourseTypeTwo()!=null) {
			coursesSeries.setCourseTypeTwo(command.getCourseTypeTwo());
		}
		if(command.getIsSeriesLesson()!=null) {
			coursesSeries.setIsSeriesLesson(command.getIsSeriesLesson());
		}
		if(StringUtils.isNotBlank(command.getCover())) {
			coursesSeries.setCover(command.getCover());
		}
		if(command.getIsPrice()==1 || command.getIsPrice()==2) {
			coursesSeries.setIsPrice(command.getIsPrice());
			/*if(command.getIsPrice()==2) {
				if(command.getPrice()>0.0) {
					coursesSeries.setPrice(command.getPrice()*100);
				}else {
					resultDto.setResultMessage("请输入费用！");
					resultDto.setSuccess(false);
					return resultDto;
				}
			}*/
		}
		coursesSeries.setCreationTime(new Date());
		if(command.getIsReview()!=null) {
			coursesSeries.setIsReview(command.getIsReview());
		}
		if(command.getIsTopping()!=null) {
			coursesSeries.setIsTopping(command.getIsTopping());
		}
		if(command.getTeacherId()!=null) {
			TeachersQo teachersQo=new TeachersQo();

			teachersQo.setSeachOrganization(true);
			teachersQo.setSeachUser(true);
			teachersQo.setTeacherId(command.getTeacherId());
			Teacher teacher=teacherDao.queryUnique(teachersQo);
			if(teacher.getCoursesNum()==null) {
				teacher.setCoursesNum(0);
			}
			teacher.setCoursesNum(teacher.getCoursesNum()+1);
			coursesSeries.setUploader(teacher);
			teacherDao.update(teacher);
			coursesSeries.setUploader(teacher);
		}else {
			resultDto.setResultMessage("上传人不能为空！");

			resultDto.setSuccess(false);
			return resultDto;
		}
		if(StringUtils.isNotBlank(command.getTotalDetails())) {
			coursesSeries.setTotalDetails(command.getTotalDetails());
		}
		if(StringUtils.isNotBlank(command.getTotalTitle())) {
			coursesSeries.setTotalTitle(command.getTotalTitle());
		}
		if(StringUtils.isNotBlank(command.getViewTime())) {
			coursesSeries.setTotalLength(command.getViewTime());
		}
		seriesDao.save(coursesSeries);
		if(coursesSeries.getId()!=null) {
			courses.setCoursesSeries(coursesSeries);
		}
		if(StringUtils.isNotBlank(command.getTotalTitle())) {
			courses.setCoursetitle(command.getTotalTitle());
		}
		if(StringUtils.isNotBlank(command.getVideo())) {
			courses.setVideo(command.getVideo());
		}
		if(command.getIsPrice()==1 || command.getIsPrice()==2) {
			courses.setIsPay(command.getIsPrice());
		}
		if(command.getTeacherId()!=null) {
			TeachersQo qo=new TeachersQo();
			qo.setId(command.getTeacherId());
			qo.setSeachOrganization(true);
			qo.setSeachUser(true);
			Teacher teacher= teacherDao.queryUnique(qo);
			courses.setUploader(teacher);

		}
		courses.setReleasetime(new Date());
		if(StringUtils.isNotBlank(command.getViewTime())) {
			courses.setViewTime(command.getViewTime());
		}
		if(command.getIsReview()!=null) {
			courses.setIsReview(command.getIsReview());
		}
		List<String> sList=new ArrayList<String>();
		sList=command.getImgList();
		for (int i=0;i<sList.size();i++) {
			CoursePicture coursePicture=new CoursePicture();
			coursePicture.setCoursesSeries(coursesSeries);
			if(command.getImgList()!=null) {
				coursePicture.setImageUrl(command.getImgList().get(i));
			}
			coursePicture.setSerialNumber(i);
			coursePictureDao.save(coursePicture);;
		}
		CoursesDao.save(courses);
		resultDto.setSuccess(true);
		return resultDto;
	}

	@Override
	public ServiceResultDto mudifyCommand(CoursesSeriesCommand comand) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		CoursesQo qo=new CoursesQo();
		CoursesSeriesQo coursesSeriesQo=new CoursesSeriesQo();
		coursesSeriesQo.setSeachTeacher(true);
		qo.setSeachCoursesSeries(true);
		qo.setSeachUploader(true);
		qo.setCoursesId(comand.getCoursesId());
		Courses courses=CoursesDao.queryUnique(qo);
		coursesSeriesQo.setCoursesSeriesId(courses.getCoursesSeries().getId());
		CoursesSeries coursesSeries2= seriesDao.queryUnique(coursesSeriesQo);
		if(comand==null) {
			resultDto.setResultMessage("非法操作");
			resultDto.setSuccess(false);
			return resultDto;
		}
		if(StringUtils.isNotBlank(comand.getCourseSecondaryTitle())) {
			coursesSeries2.setCourseSecondaryTitle(comand.getCourseSecondaryTitle());
		}
		if(comand.getCourseShare()!=null) {
			coursesSeries2.setCourseShare(comand.getCourseShare());
		}
		if(comand.getCourseTypeOne()!=null) {
			coursesSeries2.setCourseTypeOne(comand.getCourseTypeOne());
		}
		if(comand.getCourseTypeTwo()!=null) {
			coursesSeries2.setCourseTypeTwo(comand.getCourseTypeTwo());
		}
		if(StringUtils.isNotBlank(comand.getCover())) {
			coursesSeries2.setCover(comand.getCover());
		}
		if(comand.getIsPrice()==1 || comand.getIsPrice()==2) {
			coursesSeries2.setIsPrice(comand.getIsPrice());
			courses.setIsPay(comand.getIsPrice());
		}
		if(comand.getIsSeriesLesson()!=null) {
			coursesSeries2.setIsSeriesLesson(comand.getIsSeriesLesson());
		}
		if(StringUtils.isNotBlank(comand.getTotalDetails())) {
			coursesSeries2.setTotalDetails(comand.getTotalDetails());
		}
		if(StringUtils.isNotBlank(comand.getTotalTitle())) {
			coursesSeries2.setTotalTitle(comand.getTotalTitle());
			courses.setCoursetitle(comand.getTotalTitle());
		}else {
			resultDto.setResultMessage("一级标题不能为空！");
			resultDto.setSuccess(false);
			return resultDto;
		}
		if(StringUtils.isNotBlank(comand.getViewTime())) {
			coursesSeries2.setTotalLength(comand.getViewTime());
			courses.setViewTime(comand.getViewTime());
		}
		if(StringUtils.isNotBlank(comand.getVideo())) {
			courses.setVideo(comand.getVideo());
		}
		courses.setIsReview(0);
		CoursesDao.update(courses);
		seriesDao.update(coursesSeries2);
		resultDto.setSuccess(true);
		return resultDto;
	}
}
